********************************************************************************
* Fight or Flight: Endogenous timing in conflicts ******************************
* Boris van Leeuwen, Theo Offerman & Jeroen van de Ven *************************
* June 26, 2020 ****************************************************************
********************************************************************************

* FIRST RUN DATAPREP.DO

clear
cd "C:\Users\bleeuwen\Dropbox\Fight or flight\Data\analysis"

*******************************
* Analysis ********************
*******************************

use "fof.dta", clear

******************************
* 4.1 Timing of actions ******
******************************

* Figure 2: average waiting time
preserve
drop if timing==2
collapse tijd3 tijd, by(k timing round)
gen treat=timing+k
tsset treat round

tssmooth ma smtijd3=tijd3 , window(1 1 1)
twoway (tsline smtijd3 if k==-6, lcolor(gs0) lwidth(medthick) lpattern(vshortdash)) (tsline smtijd3 if k==6, lcolor(gs5) lwidth(medthick) lpattern(dash)) (tsline smtijd3 if k==12, lcolor(gs10) lwidth(medthick)) if timing==1, ytitle(time (ms)) ylabel(, nogrid) xtitle(round) legend(order(1 "k = -6" 2 "k = 6" 3 "k = 12") cols(1) region(lcolor(white)) position(3)) xsize(3) ysize(2) scale(1.5) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white) margin(large)) plotregion(fcolor(white) margin(zero))
graph save Graph "Figures\decision_speed.gph", replace
graph export "Figures\decision_speed.png", as(png) replace
graph export "Figures\decision_speed.eps", as(eps) preview(off) replace
graph export "Figures\decision_speed.pdf", as(pdf) replace
restore

*** Average decision times
tabstat tijd3 if timing==1, by(k)

*** Testing for treatment differences in timing of actions
preserve
collapse tijd3, by(k timing groupid)
ranksum tijd3 if timing==1 & k!=12, by(k)
ranksum tijd3 if timing==1 & k!=6, by(k)
ranksum tijd3 if timing==1 & k!=-6, by(k)
restore

*** Testing for time trends
preserve
gen first_last_10 = .
replace first_last_10 = 1 if round<=10
replace first_last_10 = 2 if round>30

table k first_last_10 if timing==1, contents(mean tijd3 )

collapse tijd3, by(k timing first_last_10 groupid)
keep if first_last_10 !=.
reshape wide tijd3, i(groupid) j(first_last_10)
signrank tijd31=tijd32 if timing==1 & k==-6
signrank tijd31=tijd32 if timing==1 & k==6
signrank tijd31=tijd32 if timing==1 & k==12
restore

* Figure 3: Distribution of decisions over time (by second)
gen second2=.
replace second2=second if timing==1

foreach z in -6 6 12 {
graph bar (sum) cf (sum) cflee if timing==1 & k==`z' & round<=20, over(second2, relabel(11 "T")) allcat bar(1, lcolor(gs4) fcolor(gs4)) bar(2, lcolor(gs4) fcolor(gs12)) ytitle(frequency) ylabel(0(250)550, nogrid) title("k = `z'" "first 20 rounds", color(white) box fcolor(black) lcolor(black) lwidth(medium) bexpand) legend(order(1 "fight" 2 "flee") region(lcolor(white))) graphregion(fcolor(white) lcolor(white)) plotregion(lcolor(black) lwidth(medium))
graph save Graph "Figures\decision_sec_`z'first20.gph", replace

graph bar (sum) cf (sum) cflee if timing==1 & k==`z' & round>20, over(second2, relabel(11 "T")) allcat bar(1, lcolor(gs4) fcolor(gs4)) bar(2, lcolor(gs4) fcolor(gs12)) ytitle(frequency) ylabel(0(250)550, nogrid) title("k = `z'" "final 20 rounds", color(white) box fcolor(black) lcolor(black) lwidth(medium) bexpand) legend(order(1 "fight" 2 "flee") region(lcolor(white))) graphregion(fcolor(white) lcolor(white)) plotregion(lcolor(black) lwidth(medium))
graph save Graph "Figures\decision_sec_`z'final20.gph", replace
}
 
grc1leg  "Figures\decision_sec_-6first20.gph" "Figures\decision_sec_-6final20.gph" "Figures\decision_sec_6first20.gph" "Figures\decision_sec_6final20.gph" "Figures\decision_sec_12first20.gph" "Figures\decision_sec_12final20.gph", cols(2) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scale(0.7)
graph save Graph "Figures\decision_sec.gph", replace
graph export "Figures\decision_sec.png", as(png) replace
graph export "Figures\decision_sec.eps", as(eps) preview(off) replace
graph export "Figures\decision_sec.pdf", as(pdf) replace

* Rushing: when do matches end?
tab action_period if timing==1 & k==-6

* Figure A.1 (appendix): Distribution of decisions over time (by period)
foreach z in -6 6 12 {
if(`z'<0){
	local j1 = 200
	local j2 = 600
	}
	else{
	local j1 = 50
	local j2 = 200
}
graph bar (sum) cf (sum) cflee if timing==1 & k==`z' & round<=20, over(period, relabel(51 "T")) allcat bar(1, lcolor(gs4) fcolor(gs4)) bar(2, lcolor(gs4) fcolor(gs12)) ytitle(frequency) ylabel(0(`j1')`j2', nogrid) title("k = `z'" "first 20 rounds", color(white) box fcolor(black) lcolor(black) lwidth(medium) bexpand) legend(order(1 "fight" 2 "flee") region(lcolor(white))) graphregion(fcolor(white) lcolor(white)) plotregion(lcolor(black) lwidth(medium))
graph save Graph "Figures\decision_time_`z'first20.gph", replace

graph bar (sum) cf (sum) cflee if timing==1 & k==`z' & round>20, over(period, relabel(51 "T")) allcat bar(1, lcolor(gs4) fcolor(gs4)) bar(2, lcolor(gs4) fcolor(gs12)) ytitle(frequency) ylabel(0(`j1')`j2', nogrid) title("k = `z'" "final 20 rounds", color(white) box fcolor(black) lcolor(black) lwidth(medium) bexpand) legend(order(1 "fight" 2 "flee") region(lcolor(white))) graphregion(fcolor(white) lcolor(white)) plotregion(lcolor(black) lwidth(medium))
graph save Graph "Figures\decision_time_`z'final20.gph", replace
}
  
grc1leg  "Figures\decision_time_-6first20.gph" "Figures\decision_time_-6final20.gph" "Figures\decision_time_6first20.gph" "Figures\decision_time_6final20.gph" "Figures\decision_time_12first20.gph" "Figures\decision_time_12final20.gph", cols(1) commonscheme scale(1) name(g1, replace) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
	plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white) margin(small))
	graph display g1, xsize(8) ysize(12)  scheme(s1mono) scale(.5)
graph save g1 "Figures\decision_time.gph", replace
graph export "Figures\decision_time.png", as(png) replace
graph export "Figures\decision_time.eps", as(eps) preview(off) replace
graph export "Figures\decision_time.pdf", as(pdf) replace

*********************************************
* 4.2 Frequency of battles and sorting ******
*********************************************

* Figure 4: Fraction of battles and sorting by treatment (incl. Experiment 2)
* incl. non-parameteric tests for treatment effects
preserve
collapse battle k timing, by(groupid)

table k timing, contents(mean battle )
* testing for treatment differences between static and dynamic (Exp. 1)
ranksum battle if k==12 & timing!=2, by(timing)
ranksum battle if k==6 & timing!=2, by(timing)
ranksum battle if k==-6 & timing!=2, by(timing)

* testing for treatment differences between static and dynamic (Exp. 2)
ranksum battle if k==12 & timing!=1, by(timing)
ranksum battle if k==6 & timing!=1, by(timing)
ranksum battle if k==-6 & timing!=1, by(timing)

gen se_battle=battle
collapse (mean) battle (sem) se_battle, by(k timing)

gen treat=.
replace treat=1 if timing==3 & k==-6
replace treat=2 if timing==1 & k==-6
replace treat=3 if timing==2 & k==-6

replace treat=5 if timing==3 & k==6
replace treat=6 if timing==1 & k==6
replace treat=7 if timing==2 & k==6

replace treat=9 if timing==3 & k==12
replace treat=10 if timing==1 & k==12
replace treat=11 if timing==2 & k==12

gen hibar=battle+1.96*se_battle
gen lowbar=battle-1.96*se_battle

twoway (bar battle treat if k==-6 & timing==3, fcolor(gs12) lcolor(black)) ///
       (bar battle treat if k==-6 & timing==1, fcolor(gs6) lcolor(black)) ///
       (bar battle treat if k==-6 & timing==2, fcolor(gs0) lcolor(black)) ///
       (bar battle treat if k==6 & timing==3, fcolor(gs12) lcolor(black)) ///
       (bar battle treat if k==6 & timing==1, fcolor(gs6) lcolor(black)) ///
       (bar battle treat if k==6 & timing==2, fcolor(gs0) lcolor(black)) ///
       (bar battle treat if k==12 & timing==3, fcolor(gs12) lcolor(black)) ///
       (bar battle treat if k==12 & timing==1, fcolor(gs6) lcolor(black)) ///
       (bar battle treat if k==12 & timing==2, fcolor(gs0) lcolor(black)) ///
       (rcap hibar lowbar treat, lcolor(black)), ///
	   graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
	   plotregion(fcolor(white) ifcolor(white) ilcolor(white) margin(small)) ///
       legend(order(1 "static" 2 "dynamic (exp. 1)" 3 "dynamic (exp. 2)") cols(3) region(lcolor(white)) position(3)) ///
       xlabel( 2 "k = -6" 6 "k = 6" 10 "k = 12", noticks) ///
	   ylabel(0(.2).8, angle(horizontal) format(%2.1f) nogrid) ///
       xtitle(" ") ytitle("fraction battles") ///
	   scale(1.3) xsize(4) ysize(2) 
graph save Graph "Figures\frequency_battles_new.gph", replace

restore

preserve
collapse sorting k timing, by(groupid)

table k timing, contents(mean sorting )

* testing for treatment differences between static and dynamic (Exp. 1)
ranksum sorting if k==12 & timing!=2, by(timing)
ranksum sorting if k==6 & timing!=2, by(timing)
ranksum sorting if k==-6 & timing!=2, by(timing)

* testing for treatment differences between static and dynamic (Exp. 2)
ranksum sorting if k==12 & timing!=1, by(timing)
ranksum sorting if k==6 & timing!=1, by(timing)
ranksum sorting if k==-6 & timing!=1, by(timing)

gen se_sorting=sorting
collapse (mean) sorting (sem) se_sorting, by(k timing)

gen treat=.
replace treat=1 if timing==3 & k==-6
replace treat=2 if timing==1 & k==-6
replace treat=3 if timing==2 & k==-6

replace treat=5 if timing==3 & k==6
replace treat=6 if timing==1 & k==6
replace treat=7 if timing==2 & k==6

replace treat=9 if timing==3 & k==12
replace treat=10 if timing==1 & k==12
replace treat=11 if timing==2 & k==12

gen hibar=sorting+1.96*se_sorting
gen lowbar=sorting-1.96*se_sorting

twoway (bar sorting treat if k==-6 & timing==3, fcolor(gs12) lcolor(black)) ///
       (bar sorting treat if k==-6 & timing==1, fcolor(gs6) lcolor(black)) ///
       (bar sorting treat if k==-6 & timing==2, fcolor(gs0) lcolor(black)) ///
       (bar sorting treat if k==6 & timing==3, fcolor(gs12) lcolor(black)) ///
       (bar sorting treat if k==6 & timing==1, fcolor(gs6) lcolor(black)) ///
       (bar sorting treat if k==6 & timing==2, fcolor(gs0) lcolor(black)) ///
       (bar sorting treat if k==12 & timing==3, fcolor(gs12) lcolor(black)) ///
       (bar sorting treat if k==12 & timing==1, fcolor(gs6) lcolor(black)) ///
       (bar sorting treat if k==12 & timing==2, fcolor(gs0) lcolor(black)) ///
       (rcap hibar lowbar treat, lcolor(black)), ///
	   graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
	   plotregion(fcolor(white) ifcolor(white) ilcolor(white) margin(small)) ///
       legend(order(1 "static" 2 "dynamic (exp. 1)" 3 "dynamic (exp. 2)") cols(3) region(lcolor(white)) position(3)) ///
       xlabel( 2 "k = -6" 6 "k = 6" 10 "k = 12", noticks) ///
	   ylabel(0(.2).8, angle(horizontal) format(%2.1f) nogrid) ///
       xtitle(" ") ytitle("fraction weaker escapes") ///
	   scale(1.3) xsize(4) ysize(2) 
graph save Graph "Figures\frequency_sorting_new.gph", replace

restore

grc1leg  "Figures\frequency_battles_new.gph" "Figures\frequency_sorting_new.gph", rows(1) commonscheme scale(1) name(g1, replace) pos(6) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
	plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white) margin(small))  
graph display g1, xsize(12) ysize(4) scale(1.7)
graph save g1 "Figures\battles_sorting_new.gph", replace
graph export "Figures\battles_sorting_new.png", as(png) replace
graph export "Figures\battles_sorting_new.eps", as(eps) preview(off) replace
graph export "Figures\battles_sorting_new.pdf", as(pdf) replace

* Table A.1 (appendix): Treatment differences in battles and sorting
preserve
collapse sorting battle k k_norm timing, by(groupid)

gen dynamic = .
replace dynamic = 1 if timing==1
replace dynamic = 0 if timing==3
label variable dynamic "Dynamic"

eststo clear
eststo: quietly reg battle i.k_norm##i.dynamic if timing!=2
eststo: quietly reg sorting i.k_norm##i.dynamic  if timing!=2
esttab using "Tables\fighting_sorting.tex", label compress booktabs star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) r2(3) addnote("") nobaselevels  replace
restore

* footnote 9: adjust for those who wanted to fight but were a bit slower
preserve
collapse battle battle_ties k timing, by(groupid)
table k timing if timing!=2, contents(mean battle mean battle_ties ) format(%4.3f)
restore

* Figure A.2 (appendix): waiting times for weak and strong players (appendix)
foreach z in -6 6 12 {
preserve
keep if ability<=250 | ability>=750
gen strong=.
replace strong=1 if ability >=750
replace strong=0 if ability <750
collapse tijd3 tijd2 tijd, by(k timing strong round)
keep if k==`z' & timing==1
tsset strong round
tssmooth ma smtijd3=tijd3 , window(1 1 1)
twoway (tsline smtijd3 if strong==1, lcolor(gs6) lwidth(medthick) lpattern(solid)) (tsline smtijd3 if strong==0, lcolor(gs0) lwidth(medthick) lpattern(vshortdash)), ytitle(time (ms)) ylabel(0(2000)7000, nogrid) xtitle(round) legend(order(1 "strong" 2 "weak") cols(1) region(lcolor(white)) position(3)) xsize(3) ysize(2) scale(1.5) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(black)) title("k = `z'")
graph save Graph "Figures\decision_speed_types_k`z'.gph", replace
restore
}
grc1leg  "Figures\decision_speed_types_k-6.gph" "Figures\decision_speed_types_k6.gph" "Figures\decision_speed_types_k12.gph", name(new_graph, replace) rows(1) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scale(1) position(3) ring(1)
graph display new_graph, xsize(6) ysize(2)
graph save new_graph "Figures\decision_speed_types.gph", replace
graph export "Figures\decision_speed_types.png", as(png) replace
graph export "Figures\decision_speed_types.eps", as(eps) preview(off) replace
graph export "Figures\decision_speed_types.pdf", as(pdf) replace

* Figure 5: Earnings
preserve
collapse verdiensten, by(timing k q100)
twoway (connected verdiensten q100 if timing==3, mcolor(gs6) msymbol(circle) lcolor(gs5) lpattern(vshortdash)) ///
	(connected verdiensten q100 if timing==1, mcolor(gs0) msymbol(square) lcolor(gs0) lpattern(dash)) ///
	, ytitle(mean earnings) ylabel(, angle(horizontal) nogrid) ///
	xtitle(fighting ability) ///
	xlabel(1 "0-100" 2 "101-200" 3 "201-300" 4 "301-400" 5 "401-500" 6 "501-600" 7 "601-700" 8 "701-800" 9 "801-900" 10 "901-1000", labsize(small) angle(forty_five) valuelabel) ///
	xsize(5.5) ysize(3) aspectratio(1) ///
	by(, graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
	plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))) by(k, rows(1) note(" ")) ///
	subtitle(, color(white) box fcolor(black) lcolor(black)) ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
	plotregion(fcolor(white) lcolor(black) ifcolor(white) ilcolor(white)) ///
	legend(order(1 "static" 2 "dynamic") rows(1) region(lcolor(white)) position(12))
graph save Graph "Figures\earnings.gph", replace
graph export "Figures\earnings.png", as(png) replace
graph export "Figures\earnings.eps", as(eps) preview(off) replace
graph export "Figures\earnings.pdf", as(pdf) replace
restore

* testing for treatment differences in earnings
preserve
collapse verdiensten k timing, by(groupid)
ranksum verdiensten if k==12 & timing!=2, by(timing)
ranksum verdiensten if k==6 & timing!=2, by(timing)
ranksum verdiensten if k==-6 & timing!=2, by(timing)
restore

*********************************************
* 4.3 Individual behavior *******************
*********************************************

* Figure 6: Behavior before the final period in the dynamic game
graph bar (mean) action_flight (mean) action_wait (mean) action_other (mean) action_fight if timing==1, over(q100, relabel(1 "0-100" 2 "101-200" 3 "201-300" 4 "301-400" 5 "401-500" 6 "501-600" 7 "601-700" 8 "701-800" 9 "801-900" 10 "901-1000") label(angle(forty_five))) stack bar(1, fcolor(gs16) lcolor(black) lwidth(vthin)) bar(2, fcolor(gs12) lcolor(black) lwidth(vthin)) bar(3, fcolor(gs8) lcolor(black) lwidth(vthin)) bar(4, fcolor(gs0) lcolor(black) lwidth(vthin)) ytitle(Fraction) ylabel(0(.2)1, angle(horizontal) format(%2.1f) nogrid) by(, subtitle(, position(6)) note(" ")) by(, legend(position(6))) legend(order(1 "Flee" 2 "Wait" 3 "Other moves" 4 "Fight") colfirst rows(1) region(fcolor(white) lcolor(white))) xsize(6) ysize(3) scale(1.2) aspectratio(1) by(, graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))) by(k, rows(1)) subtitle(, color(white) box fcolor(black) lcolor(black)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(margin(medsmall) fcolor(white) lcolor(black) ifcolor(white) ilcolor(white)) b1title("fighting ability")
graph save Graph "Figures\action_by_strength.gph", replace
graph export "Figures\action_by_strength.png", as(png) replace
graph export "Figures\action_by_strength.eps", as(eps) preview(off) replace
graph export "Figures\action_by_strength.pdf", as(pdf) replace

gen move = ""
replace move = "wait" if action_wait==1
replace move = "other moves" if action_other==1
replace move = "fight" if action_fight==1
replace move = "flee" if action_flight==1

tab move k if timing==1, col 

********************************************************************************
*** Appendix D: individual cutoff strategies ***********************************
********************************************************************************

* the individual cutoffs are estimated using Matlab
* generate two datafiles that are needed to estimate
gen action = .
replace action = 1 if mymove=="flight" & decision=="endo" & timing==1
replace action = 2 if mymove=="flight" & decision=="exo" & timing==1
replace action = 3 if mymove=="fight" & decision=="exo" & timing==1
replace action = 4 if mymove=="fight" & decision=="endo" & timing==1
replace action = 5 if mymove=="none"  & timing==1

replace action = 1 if mymove=="flight" & timeperiod<4 & timing==2
replace action = 2 if mymove=="flight" & timeperiod==4 & timing==2
replace action = 3 if mymove=="fight" & timeperiod==4 & timing==2
replace action = 4 if mymove=="fight" & timeperiod<4 & timing==2
replace action = 5 if mymove=="none"  & timing==2

preserve
keep if timing!=3
keep subjectid ability round
reshape wide ability, i(subjectid) j(round)
export excel using "strengths", replace
export delimited using "strengths", novarnames replace
restore

preserve
keep if timing!=3
keep subjectid action round
reshape wide action, i(subjectid) j(round)
forvalues k = 1(1)40 {
replace action`k'=9 if action`k'==.
}
export excel using "actions", replace
export delimited using "actions", novarnames replace
restore

drop action

*** compute individual cutoffs in Matlab...
*** use 'cutpoints_endo.m'

*** import estimated cutoffs...
preserve
import excel "cutoffs_final20.xlsx", sheet("Sheet1") firstrow clear
save "cutoffs_final20.dta", replace
restore

preserve
merge m:1 subjectid using "cutoffs_final20.dta"
drop _merge

gen cuts=10000*c1_f20+c2_f20 
egen freq=sum(cuts), by(cuts k)
replace freq=freq/cuts

* Figure A.5 (appendix): Estimated individual cutoffs 
foreach t in 1 {
foreach z in -6 6 12 {
	
if(`z'==-6){
	local j1 = 119
	local j2 = 119
} 
	else if(`z'==6){
	local j1 = 1000
	local j2 = 348
} 
	else {
	local j1 = 1000
	local j2 = 0	
}

twoway (scatteri 0 0 1000 1000, recast(line) lcolor(gs10)) (scatteri `j1' `j2', mcolor(black) msize(vlarge) mfcolor(black) msymbol(S)) (scatter c3_f20 c1_f20 [fweight = freq], mcolor(gs4) msize(medlarge) mfcolor(none))  if k==`z' & timing==`t', ytitle(c3) ytitle(, size(medlarge)) ylabel(0(250)1000, labsize(medlarge)) xtitle(c1) xtitle(, size(medlarge)) xlabel(0(250)1000, labsize(medlarge) grid)  xsize(3) ysize(3.3)  subtitle(, color(white) box fcolor(black) lcolor(black) lwidth(medthick)) graphregion(fcolor(white) lcolor(white)) plotregion(lcolor(black) lwidth(medium)) legend(order(2 "theoretical prediction" 3 "indiv. estimates") region(lcolor(white))) title(k = `z', color(white) box fcolor(black) lcolor(black) lwidth(medium) bexpand) aspect(1)
graph save Graph "Figures\indiv_cut`t'_`z'.gph", replace
}
 
grc1leg  "Figures\indiv_cut`t'_-6.gph" "Figures\indiv_cut`t'_6.gph" "Figures\indiv_cut`t'_12.gph", name(new_graph2, replace) rows(1) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scale(1.2) position(6) ring(0)
graph display new_graph2, xsize(5.5) ysize(2.5)
graph save Graph "Figures\cuts`t'.gph", replace
graph export "Figures\cuts`t'.png", as(png) replace
graph export "Figures\cuts`t'.eps", as(eps) preview(off) replace
graph export "Figures\cuts`t'.pdf", as(pdf) replace
}

* Table A.11 (appendix): Estimated cutoffs 
keep if round==1

gen c1_c3=.
replace c1_c3 = 1 if c1_f20 == c3_f20
replace c1_c3 = 0 if c1_f20 != c3_f20

table k if round==1 & timing==1, contents(mean c1_f20 mean c2_f20 mean c3_f20 mean correct_f20 mean c1_c3) format(%3.2f)

* Tables A.2 & A.3 (appendix): Fleeing and fighting regressions based on estimated cutoffs
foreach t in 1 {
eststo clear
eststo: quietly reg c1_f20 lotteries_rejected female z_dominance z_strength if timing==`t' & k==-6, cluster(groupid)
eststo: quietly reg c1_f20 lotteries_rejected female z_dominance z_strength if timing==`t' & k==6, cluster(groupid)
eststo: quietly reg c1_f20 lotteries_rejected female z_dominance z_strength if timing==`t' & k==12, cluster(groupid)
eststo: quietly reg c1_f20 lotteries_rejected female z_dominance z_strength i.k_norm if timing==`t', cluster(groupid)
esttab using "Tables\fleeing_cutoffs`t'.tex", label booktabs compress width(0.8\hsize)  star(* 0.10 ** 0.05 *** 0.01) se(2) b(2) r2(2) addnote("") replace

eststo clear
eststo: quietly reg c3_f20 lotteries_rejected female z_dominance z_strength if timing==`t' & k==-6, cluster(groupid)
eststo: quietly reg c3_f20 lotteries_rejected female z_dominance z_strength if timing==`t' & k==6, cluster(groupid)
eststo: quietly reg c3_f20 lotteries_rejected female z_dominance z_strength if timing==`t' & k==12, cluster(groupid)
eststo: quietly reg c3_f20 lotteries_rejected female z_dominance z_strength i.k_norm if timing==`t', cluster(groupid)
esttab using "Tables\fighting_cutoffs`t'.tex", label booktabs compress width(0.8\hsize)  star(* 0.10 ** 0.05 *** 0.01) se(2) b(2) r2(2) addnote("") replace
}
restore

* Table 2: Fleeing before endgame
eststo clear
xtset subjectid round
quietly xtprobit flee_before_end lotteries_rejected female z_dominance z_strength ability_01 round if timing==1 & k==-6, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_min6
quietly xtprobit flee_before_end lotteries_rejected female z_dominance z_strength ability_01 round if timing==1 & k==6, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_6
quietly xtprobit flee_before_end lotteries_rejected female z_dominance z_strength ability_01 round if timing==1 & k==12, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_12
quietly xtprobit flee_before_end lotteries_rejected female z_dominance z_strength ability_01 round i.k_norm if timing==1, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_all
esttab using "Tables\fleeing_probit.tex", label compress nobaselevels booktabs star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) addnote("") replace

* Table A.4 (appendix): Fleeing before endgame, adding or removing controls
eststo clear
xtset subjectid round
quietly xtprobit flee_before_end lotteries_rejected female z_dominance z_strength ability_01 round i.k_norm if timing==1, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_base
quietly xtprobit flee_before_end lotteries_rejected ability_01 round i.k_norm if timing==1, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_nocontrols
quietly xtprobit flee_before_end lotteries_rejected female z_dominance z_strength z_prestige z_masculinity ability_01 round i.k_norm if timing==1, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_more
esttab using "Tables\fleeing_probit_controls.tex", label compress booktabs star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) addnote("") replace

* Table 3: Fighting in the first second
eststo clear
xtset subjectid round
quietly xtprobit fight_first_second lotteries_rejected female z_dominance z_strength ability_01 round if timing==1 & k==6, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_6_all
quietly xtprobit fight_first_second lotteries_rejected female z_dominance z_strength ability_01 round if timing==1 & k==12, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_12_all
quietly xtprobit fight_first_second lotteries_rejected female z_dominance z_strength ability_01 round i.k_norm if timing==1 & k>0, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_pos_all
quietly xtprobit fight_first_second lotteries_rejected female z_dominance z_strength ability_01 round if timing==1 & k==6 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_6_final20
quietly xtprobit fight_first_second lotteries_rejected female z_dominance z_strength ability_01 round if timing==1 & k==12 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_12_final20
quietly xtprobit fight_first_second lotteries_rejected female z_dominance z_strength ability_01 round i.k_norm if timing==1 & k>4 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_pos_final20
esttab using "Tables\fighting_firstsecond_probit.tex", label compress nobaselevels booktabs star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) addnote("") replace

* Table A.5 (appendix): Fighting in the first second, adding or removing controls
eststo clear
xtset subjectid round
quietly xtprobit fight_first_second lotteries_rejected female z_dominance z_strength ability_01 round i.k_norm if timing==1 & k>4 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_pos_base
quietly xtprobit fight_first_second z_dominance ability_01 round i.k_norm if timing==1 & k>4 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_pos_nocontrols
quietly xtprobit fight_first_second lotteries_rejected female z_dominance z_strength z_prestige z_masculinity ability_01 round i.k_norm if timing==1 & k>4 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_pos_more
esttab using "Tables\fighting_probit_controls.tex", label compress booktabs star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) addnote("") replace


*********************************************
* 4.4 Experiment 2 **************************
*********************************************

* Figure 7: Distribution of decisions over time (by period)
foreach z in -6 6 12 {
graph bar (sum) cf (sum) cflee if timing==2 & k==`z' & round<=20, over(timeperiod) allcat bar(1, lcolor(gs4) fcolor(gs4)) bar(2, lcolor(gs4) fcolor(gs12)) ytitle(frequency) ylabel(0(250)550, nogrid) title("k = `z'" "first 20 rounds", color(white) box fcolor(black) lcolor(black) lwidth(medium) bexpand) legend(order(1 "fight" 2 "flee") region(lcolor(white))) graphregion(fcolor(white) lcolor(white)) plotregion(lcolor(black) lwidth(medium))
graph save Graph "Figures\decision_sec_`z'first20_new.gph", replace

graph bar (sum) cf (sum) cflee if timing==2 & k==`z' & round>20, over(timeperiod) allcat bar(1, lcolor(gs4) fcolor(gs4)) bar(2, lcolor(gs4) fcolor(gs12)) ytitle(frequency) ylabel(0(250)550, nogrid) title("k = `z'" "final 20 rounds", color(white) box fcolor(black) lcolor(black) lwidth(medium) bexpand) legend(order(1 "fight" 2 "flee") region(lcolor(white))) graphregion(fcolor(white) lcolor(white)) plotregion(lcolor(black) lwidth(medium))
graph save Graph "Figures\decision_sec_`z'final20_new.gph", replace
}
 
grc1leg  "Figures\decision_sec_-6first20_new.gph" "Figures\decision_sec_-6final20_new.gph" "Figures\decision_sec_6first20_new.gph" "Figures\decision_sec_6final20_new.gph" "Figures\decision_sec_12first20_new.gph" "Figures\decision_sec_12final20_new.gph", cols(2) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scale(0.7)
graph save Graph "Figures\decision_new.gph", replace
graph export "Figures\decision_new.png", as(png) replace
graph export "Figures\decision_new.eps", as(eps) preview(off) replace
graph export "Figures\decision_new.pdf", as(pdf) replace

* Figure A.3 (appendix): average waiting time (Experiment 2)
preserve
drop if timing!=2
collapse timeperiod, by(k round)

tsset k round

tssmooth ma smtijd3=timeperiod , window(1 1 1)
twoway (tsline smtijd3 if k==-6, lcolor(gs0) lwidth(medthick) lpattern(vshortdash)) (tsline smtijd3 if k==6, lcolor(gs5) lwidth(medthick) lpattern(dash)) (tsline smtijd3 if k==12, lcolor(gs10) lwidth(medthick)), ytitle(time (period)) ylabel(, nogrid) xtitle(round) legend(order(1 "k = -6" 2 "k = 6" 3 "k = 12") cols(1) region(lcolor(white)) position(3)) xsize(3) ysize(2) scale(1.5) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white) margin(large)) plotregion(fcolor(white) margin(zero))
graph save Graph "Figures\decision_speed_new.gph", replace
graph export "Figures\decision_speed_new.png", as(png) replace
graph export "Figures\decision_speed_new.eps", as(eps) preview(off) replace
graph export "Figures\decision_speed_new.pdf", as(pdf) replace
restore

*** Testing for time trends
preserve
gen first_last_10 = .
replace first_last_10 = 1 if round<=10
replace first_last_10 = 2 if round>30

table k first_last_10 if timing==2, contents(mean timeperiod )

collapse timeperiod, by(k timing first_last_10 groupid)
keep if first_last_10 !=.
reshape wide timeperiod, i(groupid) j(first_last_10)
signrank timeperiod1=timeperiod2 if timing==2 & k==-6
signrank timeperiod1=timeperiod2 if timing==2 & k==6
signrank timeperiod1=timeperiod2 if timing==2 & k==12
restore

* Table A.6 (appendix): Treatment differences in battles and sorting
preserve
collapse sorting battle k k_norm timing, by(groupid)

gen dynamic = .
replace dynamic = 1 if timing==2
replace dynamic = 0 if timing==3
label variable dynamic "Dynamic"

eststo clear
eststo: quietly reg battle i.k_norm##i.dynamic if timing!=1
eststo: quietly reg sorting i.k_norm##i.dynamic  if timing!=21
esttab using "Tables\fighting_sorting_new.tex", label compress booktabs star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) r2(3) addnote("") nobaselevels  replace
restore

* Table A.7 (appendix): Fighting in the first period (Experiment 2)
eststo clear
xtset subjectid round
quietly xtprobit fight_first_period lotteries_rejected female z_dominance contest z_jow_sr mistakes  ability_01 round if timing==2 & k==6, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_6_all
quietly xtprobit fight_first_period lotteries_rejected female z_dominance contest z_jow_sr mistakes  ability_01 round if timing==2 & k==12, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_12_all
quietly xtprobit fight_first_period lotteries_rejected female z_dominance contest z_jow_sr mistakes  ability_01 round i.k_norm if timing==2 & k>0, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_pos_all
quietly xtprobit fight_first_period lotteries_rejected female z_dominance contest z_jow_sr mistakes  ability_01 round if timing==2 & k==6 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_6_final20
quietly xtprobit fight_first_period lotteries_rejected female z_dominance contest z_jow_sr mistakes  ability_01 round if timing==2 & k==12 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_12_final20
quietly xtprobit fight_first_period lotteries_rejected female z_dominance contest z_jow_sr mistakes  ability_01 round i.k_norm if timing==2 & k>0 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_pos_final20
esttab using "Tables\fighting_firstperiod_probit_new.tex", label compress nobaselevels booktabs star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) addnote("") replace

* Table A.8 (appendix): Fleeing before endgame (Experiment 2)
eststo clear
xtset subjectid round
quietly xtprobit flee_before_end lotteries_rejected female z_dominance contest z_jow_sr mistakes ability_01 round if timing==2 & k==-6, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_min6
quietly xtprobit flee_before_end lotteries_rejected female z_dominance contest z_jow_sr mistakes  ability_01 round if timing==2 & k==6, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_6
quietly xtprobit flee_before_end lotteries_rejected female z_dominance contest z_jow_sr mistakes  ability_01 round if timing==2 & k==12, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_12
quietly xtprobit flee_before_end lotteries_rejected female z_dominance contest z_jow_sr mistakes  ability_01 round i.k_norm if timing==2, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_k_all
esttab using "Tables\fleeing_probit_new.tex", label compress nobaselevels booktabs star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) addnote("") replace

* Table A.9 (appendix): Fighting early in the game (Experiments 1 and 2 combined)
gen early_fighting=.
replace early_fighting=fight_first_second if timing==1
replace early_fighting=fight_first_period if timing==2

eststo clear
xtset subjectid round
quietly xtprobit early_fighting lotteries_rejected female z_dominance ability_01 round i.k_norm if timing==1 & k>0 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_1_final20
quietly xtprobit early_fighting lotteries_rejected female z_dominance ability_01 round i.k_norm if timing==2 & k>0 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_2_final20
quietly xtprobit early_fighting lotteries_rejected female z_dominance ability_01 round i.k_norm i.timing if timing!=3 & k>0 & round>20, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_all_final20
esttab using "Tables\early_fighting.tex", label compress nobaselevels booktabs star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) addnote("") replace

* Table A.10 (appendix): Fleeing before endgame (Experiments 1 and 2 combined)
eststo clear
quietly xtprobit flee_before_end lotteries_rejected female z_dominance ability_01 round i.k_norm if timing==1, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_1
quietly xtprobit flee_before_end lotteries_rejected female z_dominance ability_01 round i.k_norm if timing==2, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_2
quietly xtprobit flee_before_end lotteries_rejected female z_dominance ability_01 round i.k_norm i.timing if timing!=3, vce(cluster groupid) re
estpost margins, dydx(*)
eststo me_all
esttab using "Tables\fleeing_probit_new_v2.tex", label compress nobaselevels booktabs star(* 0.10 ** 0.05 *** 0.01) se(3) b(3) addnote("") replace
